﻿using ManaSky.Api.Controllers;
using ManaSky.Common.Helpers;
using ManaSky.Common.Infos;
using ManaSky.Common.Models;
using ManaSky.Entity;
using ManaSky.IService;
using ManaSky.Model;
using ManaSky.Model.Lonin.Response;
using ManaSky.Service;
using ManaSky.SSO.Helpers;
using ManaSky.SSO.Interface;
using ManaSky.SSO.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NuGet.Common;

namespace ManaSky.Api.Controllers
{
    /// <summary>
    /// 登陆
    /// </summary>
    [Route("ManaSky/[controller]")]
    public class LoginController : BaseController
    {
        private readonly ISysUsersService _sysUsersService;
        public LoginController(IConfiguration configuration,IAuth auth, ISysUsersService sysUsersService)
            :base(configuration,auth)
        {
            _sysUsersService = sysUsersService;
        }

        [HttpPost,Route("login"), AllowAnonymous]
        public async Task<IActionResult> Login([FromBody]LoginRequest request)
        {
            ApiRequest apiRequest = new ApiRequest();
            if (request == null)
                return new JsonResult(ApiRequest.Fail("请联系开发人员!"));
            if (string.IsNullOrWhiteSpace(request.LoginName))
                return new JsonResult(ApiRequest.Fail("账号不能为空!"));
            if (string.IsNullOrWhiteSpace(request.Password))
                return new JsonResult(ApiRequest.Fail("密码不能为空!"));
            try
            {
                var userInfoResult = await _sysUsersService.GetUserByName(request.LoginName);
                if (userInfoResult.Success)
                {
                    var userInfo = (sys_user)userInfoResult.ReturnData;
                    if (userInfo == null)
                        return new JsonResult(ApiRequest.Fail("系统异常，请联系工作人员！"));
                    var pwd = EncryptHelper.DecryptAES(request.Password);
                    pwd = EncryptHelper.EncryptByMD5(pwd);
                    if (userInfo.Password == pwd)
                    {
                        if (userInfo.IsEnable == 1)
                            return new JsonResult(ApiRequest.Fail("用户被禁用，请联系工作人员！"));

                        var result = await _sysUsersService.UserLogin(userInfo);
                        if (!result.Success)
                            return new JsonResult(result.ToRequest());
                        var tokenManagement = _configuration.GetSection("JwtTokens").Get<JwtConfig>();
                        var currUser = new CurrentUserInfo();
                        currUser.UserID = userInfo.Id;
                        currUser.UserName = userInfo.LoginName;
                        var tokenInfo = JwtHelper.CreateToken(tokenManagement, currUser);
                        LoginResponse response = new LoginResponse();
                        response.Id = userInfo.Id;
                        response.Super = userInfo.Super;
                        response.Avatar = userInfo.Avatar;
                        response.LastLoginTime = userInfo.LastLoginTime;
                        response.NickName = userInfo.LoginName;
                        response.UserName = userInfo.Name;
                        response.Token = tokenInfo.Token;
                        return new JsonResult(ApiRequest.Success(response));
                    }
                    else
                        return new JsonResult(ApiRequest.Fail("账号或密码不对！"));
                }
                else
                {
                    //return userInfoResult.ToRequest();
                    return new JsonResult(ApiRequest.Fail("系统异常，请联系工作人员！"));
                }
            }
            catch (Exception ex)
            {
                apiRequest.GetFail(ex.Message);
            }
            return new JsonResult(apiRequest); ;
        }
    }
}  
